package cn.springcloud.fix.samples.controller;

import cn.springcloud.fix.common.cache.annotation.AutoIdempotent;
import cn.springcloud.fix.common.cache.annotation.RedisCache;
import cn.springcloud.fix.common.cache.annotation.RedisEvict;
import cn.springcloud.fix.common.cache.service.RedisService;
import cn.springcloud.fix.common.core.base.BaseResponse;
import cn.springcloud.fix.samples.config.CommonProperties;
import cn.springcloud.fix.samples.vo.User;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CommonProperties commonProperties;

    /**
     * 幂等请求
     */
    @AutoIdempotent
    @RequestMapping("/testIdempotent")
    public BaseResponse testIdempotent() {
        return BaseResponse.SUCCESS();
    }

    @RedisCache(pre = "prefix", key = "testRedisCache")
    @RequestMapping("/testRedisCache")
    public BaseResponse testRedisCache() {
        return BaseResponse.SUCCESS();
    }

    /**
     * 指定key清除
     */
    @RedisEvict(pre = "prefix", key = "testRedisCache")
    @RequestMapping("/testRedisEvict")
    public BaseResponse testRedisEvict() {
        return BaseResponse.SUCCESS();
    }

    /**
     * 模糊key清除
     */
    @RedisEvict(pre = "prefix", pattern = "testRedis")
    @RequestMapping("/testRedisEvict2")
    public BaseResponse testRedisEvict2() {
        return BaseResponse.SUCCESS();
    }

    @RequestMapping("/test")
    public void test() {
        redisTemplate.opsForValue().set("userkey", new User(1, "张三", 25));
        User user = (User) redisTemplate.opsForValue().get("userkey");
        User user2 = redisService.get("userkey");
        log.info("当前获取对象：{}", user.toString());
        log.info(JSON.toJSONString(commonProperties));
    }
}
